package com.cc.crm.web;

import com.cc.crm.entity.SysUser;
import com.cc.crm.service.SysUserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-12-28 16:47
 */
@Controller
@RequestMapping("/index")
public class LoginController {
    final Logger logger = LogManager.getLogger(LoginController.class.getName());
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(SysUser user, HttpServletRequest request){
        logger.debug("========login===user:"+user);
        //1.调用service层登录的方法
        SysUser lgnUser = sysUserService.login(user);
        //2.获取用户所拥有的权限，将其保存session
        HttpSession session =request.getSession();
        String rights = sysUserService.findRoleAndRight(lgnUser.getUsrRoleId());
        //2.1.保存权限
        session.setAttribute("rights", rights);
        //2.2.保存用户信息
        session.setAttribute("lgnUser",lgnUser.getUsrName());
        //3.跳转首页--重定向 index是指Controller的方法映射
        return "redirect:index";
    }
    @RequestMapping("/index")
    public String Index(){
        return "index";//jsp
    }
    @RequestMapping("/top")
    public String toTop(){
        return "top";
    }

    @RequestMapping("/index-ec")
    public String toIndex(){
        return "index-ec";//jsp
    }

    @RequestMapping("/menu")
    public String toMenu(){
        return "menu";
    }

    @RequestMapping("/mywork")
    public String toMywork(){
        return "mywork";
    }

    @RequestMapping("/footer")
    public String toFooter(){
        return "footer";
    }
}
